#include "SDLGraphics.h"
SDLGraphics::SDLGraphics(int windowWidth, int windowHeight,
                         const char* windowTitle,
                         int bgR, int bgG, int bgB)
{
   SDL_Init(SDL_INIT_VIDEO);

   TTF_Init();

   m_screen = SDL_SetVideoMode(windowWidth, windowHeight, 0, SDL_HWSURFACE | SDL_DOUBLEBUF);

   SDL_WM_SetCaption(windowTitle, 0);

   setBackgroundColor(bgR, bgG, bgB);
   
}

SDLGraphics::~SDLGraphics()
{
   TTF_Quit();
   SDL_Quit();
}

SDL_Surface* SDLGraphics::loadImage(const char* ImageFileName, int transparentRed, int transparentGreen, int transparentBlue)
{
	SDL_Surface* temp = IMG_Load(ImageFileName);
	SDL_SetColorKey(temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(temp->format, transparentRed, transparentGreen, transparentBlue));
	SDL_Surface* bitmap = SDL_DisplayFormatAlpha(temp);
	SDL_FreeSurface(temp);
	
   cout << "image loaded..." <<endl;
   return bitmap;
}
SDL_Surface* SDLGraphics::createMap(char map[][255],int colsize,int rowsize)
{
	SDL_Surface* temp = SDL_CreateRGBSurface(SDL_SWSURFACE, rowsize*32, colsize*32, i_tilegreen->format->BitsPerPixel, i_tilegreen->format->Rmask, i_tilegreen->format->Gmask, i_tilegreen->format->Bmask, i_tilegreen->format->Amask);
	SDL_SetColorKey(temp, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(temp->format, 0, 0, 0));
	ShadowLayer = SDL_CreateRGBSurface(SDL_SWSURFACE, rowsize*32, colsize*32, i_tilegreen->format->BitsPerPixel, i_tilegreen->format->Rmask, i_tilegreen->format->Gmask, i_tilegreen->format->Bmask, i_tilegreen->format->Amask);
	SDL_SetColorKey(ShadowLayer, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(ShadowLayer->format, 0, 0, 0));
	for(int y = 0; y<colsize; y++)
			{
				for(int x = 0; x<rowsize; x++)
				{
					   SDL_Rect dstRect;
						dstRect.x = x*32;
					    dstRect.y = y*32;

					if(map[x][y] == '0')
					{
						SDL_BlitSurface(i_tilegreen, 0, temp, &dstRect);
					}
					if(map[x][y] == '1')
					{
						SDL_BlitSurface(i_dirttileset[4], 0, temp, &dstRect);
						if(y>=1)
						{
							if(map[x][y-1] =='0')
								{
								SDL_BlitSurface(i_dirttileset[1],0, temp, &dstRect);
								}
						}
						if(x>=1)
						{
							if(map[x-1][y] =='0')
								{
								SDL_BlitSurface(i_dirttileset[3],0, temp, &dstRect);
								}
						}
						if(y<colsize)
						{
							if(map[x][y+1] =='0')
								{
								SDL_BlitSurface(i_dirttileset[7],0, temp, &dstRect);
								}
						}
						if(x<rowsize)
						{
							if(map[x+1][y] =='0')
								{
								SDL_BlitSurface(i_dirttileset[5],0, temp, &dstRect);
								}
						}
					}
					if(map[x][y] == '3')
					{
						SDL_BlitSurface(i_tilezombie,0, temp, &dstRect);
					}
					if(map[x][y] == '4')
					{
						SDL_BlitSurface(i_tilegreen,0, temp, &dstRect);
							player_origin_x = x*32;
							player_origin_y = y*32;
					}
				}
			}

	//shadows and such :3
		for(int y = 0; y<colsize; y++)
			{
				for(int x = 0; x<rowsize; x++)
				{
					   SDL_Rect dstRect;
						dstRect.x = x*32;
					    dstRect.y = y*32;
					if(map[x][y] == '2')
					{
						SDL_BlitSurface(i_boxalpha, 0, temp, &dstRect);
						SDL_BlitSurface(i_tilered,0, temp, &dstRect);
						g_col->addBox(dstRect.x,dstRect.y,32,32);
						//cout<< "box#" <<g_col->counter<<endl;
						//cout<< "## x: "<< dstRect.x << " | y: " << dstRect.y <<endl;
					}
				}
		}
	 //temp = IMG_Load("rsc/tile_red.png");
		SDL_Surface* bitmap = SDL_DisplayFormat(temp);
		SDL_FreeSurface(temp);
	return bitmap;
}
   int SDLGraphics::get_origin_x()
   {
	   return player_origin_x;
   }
   int SDLGraphics::get_origin_y()
   {
	   return player_origin_y;
   }
void SDLGraphics::rotateSurface(SDL_Surface* sur, int degrees, SDL_Surface* temp[])
{	
	int deg = abs(360/degrees);
	for(int i=0;i<=degrees; i++)
	{
		temp[i] = SDL_DisplayFormat(rotozoomSurfaceXY(sur, 360-i*deg, 1,1, SMOOTHING_OFF));
	}   
}
void SDLGraphics::seperateTileset(SDL_Surface* sur, int frames, int tilewidth, int tileheight,SDL_Surface* temp[])
{
	int rows = sur->w/tilewidth;
	int cols = sur->h/tileheight;
	int counter = 0;

	if(counter<=frames){
		for(int y = 0; y<cols; y++)
			{
				for(int x = 0; x<rows; x++)
				{
					temp[counter] = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, tilewidth, tileheight, sur->format->BitsPerPixel, 0, 0, 0, 0);

					SDL_SetColorKey(temp[counter], SDL_SRCCOLORKEY, SDL_MapRGB(temp[counter]->format, 0, 0, 0));	
					SDL_Rect srcRect;
						srcRect.x = x*32;
					    srcRect.y = y*32;
						srcRect.w = tilewidth;
					    srcRect.h = tileheight;

						SDL_Rect dstRect;
						dstRect.x = 0;
					    dstRect.y = 0;
						SDL_BlitSurface(sur , &srcRect, temp[counter], &dstRect);
					counter++;
				}
		}
	}
}
void SDLGraphics::closeBitmap(SDL_Surface* bitmap)
{
   SDL_FreeSurface(bitmap);
}


void SDLGraphics::beginScene()
{
   // Clear the screen
   SDL_FillRect(m_screen, NULL,SDL_MapRGB(m_screen->format, m_backgroundColorRed, m_backgroundColorGreen,m_backgroundColorBlue));
   
}

// This just displays the scene.
void SDLGraphics::endScene()
{
   SDL_Flip(m_screen);
   SDL_Flip(ShadowLayer);
}

 void SDLGraphics::addShadow(SDL_Surface* shadow, int x, int y)
 {
	SDL_Rect dstRect;
	dstRect.x = x;
	dstRect.y = y;
	SDL_BlitSurface(shadow, 0, m_screen, &dstRect);
 }

void SDLGraphics::drawSprite(SDL_Surface* imageSurface, int dstX, int dstY)
{

   SDL_Rect dstRect;
   dstRect.x = dstX;
   dstRect.y = dstY;

   SDL_BlitSurface(imageSurface, 0,m_screen , &dstRect);
}

void SDLGraphics::drawLine(int srcX, int srcY, int dstX, int dstY, int r, int g, int b, int a)
{
    lineRGBA(m_screen, srcX,srcY,dstX,dstY,r,g,b,a);
}
void SDLGraphics::drawRect(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
{
	rectangleRGBA(m_screen, x1,  y1, x2, y2, r, g, b, a);
}

void SDLGraphics::drawText(const char* string,
                           int size,
                           int x, int y,
                           int fR, int fG, int fB,
                           int bR, int bG, int bB)
{
   TTF_Font* font = TTF_OpenFont("rsc/ARIAL.TTF", size);

   SDL_Color foregroundColor = { fR, fG, fB };
   SDL_Surface* textSurface = TTF_RenderText_Solid(font, string, foregroundColor);

   SDL_Rect textLocation = { x, y, 0, 0 };

   SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);

   SDL_FreeSurface(textSurface);

   TTF_CloseFont(font);
}

void SDLGraphics::setBackgroundColor(int r, int g, int b)
{
   m_backgroundColorRed = r;
   m_backgroundColorGreen = g;
   m_backgroundColorBlue = b;
}
